/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package dao;

import bentity.bePrivilegio;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

/**
 *
 * @author luis
 */
public class daoPrivilegio {
    protected Connection conexion;

    public ArrayList<bePrivilegio> queryAll(){
        conexion = daoHelper.dameConexion();
        String sql = "SELECT * FROM PRIVILEGIO";
        ArrayList<bePrivilegio> listaPrivilegios = new ArrayList<bePrivilegio>();

        try {
            PreparedStatement ps = conexion.prepareStatement(sql);
            ResultSet rs = ps.executeQuery();

            while (rs.next()){
                bePrivilegio privilegio = new bePrivilegio();
                privilegio = new bePrivilegio(rs.getInt(1),rs.getString(2));
                listaPrivilegios.add(privilegio);
            }
            rs.close();
            ps.close();
            conexion.close();
        }
        catch (Exception e) {
            System.out.print(e.getMessage());
            return null;
        }
        return listaPrivilegios;
    }

    public bePrivilegio queryById(int idPrivilegio){
        conexion = daoHelper.dameConexion();
        String sql = "SELECT * FROM PRIVILEGIO WHERE id_Privilegio = ?";
        bePrivilegio privilegio = new bePrivilegio();

        try {
            PreparedStatement ps = conexion.prepareStatement(sql);
            ps.setInt(1, idPrivilegio);

            ResultSet rs = ps.executeQuery();
            while (rs.next()){
                privilegio = new bePrivilegio(rs.getInt(1),rs.getString(2));
            }
            rs.close();
            ps.close();
            conexion.close();
        }
        catch (Exception e) {
            System.out.print(e.getMessage());
        }
        return privilegio;

    }

    public bePrivilegio queryByNombre (String nombre){
        conexion = daoHelper.dameConexion();
        String sql = "SELECT * FROM PRIVILEGIO WHERE descripcion = ?";
        bePrivilegio privilegio = new bePrivilegio();

        try {
            PreparedStatement ps = conexion.prepareStatement(sql);
            ps.setString(1, nombre);

            ResultSet rs = ps.executeQuery();
            while (rs.next()){
                privilegio = new bePrivilegio(rs.getInt(1),rs.getString(2));
            }
            rs.close();
            ps.close();
            conexion.close();
        }
        catch (Exception e) {
            System.out.print(e.getMessage());
        }
        return privilegio;
    }
}
